{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------------- info --------------------\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 891 entries, 0 to 890\n",
      "Data columns (total 12 columns):\n",
      "PassengerId    891 non-null int64\n",
      "Survived       891 non-null int64\n",
      "Pclass         891 non-null int64\n",
      "Name           891 non-null object\n",
      "Sex            891 non-null object\n",
      "Age            714 non-null float64\n",
      "SibSp          891 non-null int64\n",
      "Parch          891 non-null int64\n",
      "Ticket         891 non-null object\n",
      "Fare           891 non-null float64\n",
      "Cabin          204 non-null object\n",
      "Embarked       889 non-null object\n",
      "dtypes: float64(2), int64(5), object(5)\n",
      "memory usage: 83.6+ KB\n",
      "None\n",
      "-------------------- describe --------------------\n",
      "       PassengerId    Survived      Pclass         Age       SibSp  \\\n",
      "count   891.000000  891.000000  891.000000  714.000000  891.000000   \n",
      "mean    446.000000    0.383838    2.308642   29.699118    0.523008   \n",
      "std     257.353842    0.486592    0.836071   14.526497    1.102743   \n",
      "min       1.000000    0.000000    1.000000    0.420000    0.000000   \n",
      "25%     223.500000    0.000000    2.000000   20.125000    0.000000   \n",
      "50%     446.000000    0.000000    3.000000   28.000000    0.000000   \n",
      "75%     668.500000    1.000000    3.000000   38.000000    1.000000   \n",
      "max     891.000000    1.000000    3.000000   80.000000    8.000000   \n",
      "\n",
      "            Parch        Fare  \n",
      "count  891.000000  891.000000  \n",
      "mean     0.381594   32.204208  \n",
      "std      0.806057   49.693429  \n",
      "min      0.000000    0.000000  \n",
      "25%      0.000000    7.910400  \n",
      "50%      0.000000   14.454200  \n",
      "75%      0.000000   31.000000  \n",
      "max      6.000000  512.329200  \n",
      "-------------------- isnull --------------------\n",
      "PassengerId      0\n",
      "Survived         0\n",
      "Pclass           0\n",
      "Name             0\n",
      "Sex              0\n",
      "Age            177\n",
      "SibSp            0\n",
      "Parch            0\n",
      "Ticket           0\n",
      "Fare             0\n",
      "Cabin          687\n",
      "Embarked         2\n",
      "dtype: int64\n",
      "age is null 177\n",
      "age is null 0\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGxpYi5vcmcvpW3flQAAEW1JREFUeJzt3X+s1fV9x/Hne8pALFMQ/HWhgy7E\n1VmkjiIOo1QHolkKpsOoiyWNy10N3ahtuihLVpumpiaudYYN56arNdXKap3G2B/IapotaouWWhR/\nIDLuVRBKrZq1GMD3/jjfu96yK/dyzj33e+7H5yM5Oef7ud9zvi/u+d4X537O93xvZCaSpHL9Vt0B\nJEntZdFLUuEsekkqnEUvSYWz6CWpcBa9JBXOopekwln0klQ4i16SCndk3QEAJk+enNOnT687hiSN\nKk888cTPMnPKYOt1RNFPnz6dDRs21B1DkkaViPjvoazn1I0kFc6il6TCWfSSVLiOmKOXpGbt27eP\n3t5e9u7dW3eUthk3bhxTp05lzJgxTd3fopc0qvX29jJhwgSmT59ORNQdZ9hlJnv27KG3t5cZM2Y0\n9RhO3Uga1fbu3ctxxx1XZMkDRATHHXdcS7+xWPSSRr1SS75Pq/8+i16SCuccvaSiXHL5FfS8vHPY\nHm9a14msvevOYXu8Olj0OizD/UM0VCX8sGlk9Ly8k4VXXT9sj7duzaphe6y6WPQ6LMP9QzRUJfyw\nqVxLly6lp6eHvXv3snLlSrq7uwG47bbbuOGGGzj55JOZOXMmY8eOZfXq1ezevZtPfOITbN++HYCb\nbrqJ+fPnty2fRS9JLbr99tuZNGkSv/rVr/jQhz7ERz/6Ud566y2+8IUv8OSTTzJhwgTOO+88Tj/9\ndABWrlzJ1Vdfzdlnn8327du54IIL2Lx5c9vyWfSS1KKbb76Z++67D4Cen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.figure.Figure at 0x7fba5284d2e8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.mlab as mlab\n",
    "\n",
    "def assign_random_values(series):\n",
    "    mean = series.mean()\n",
    "    std = series.std()\n",
    "    is_null = series.isnull().sum()\n",
    "    # compute random numbers between the mean, std and is_null\n",
    "    rand_values = np.random.randint(mean - std, mean + std, size = is_null)\n",
    "    # fill NaN values with random values generated\n",
    "    series_slice = series.copy()\n",
    "    series_slice[np.isnan(series)] = rand_values\n",
    "    series = series_slice\n",
    "    #series[np.isnan(series)] = rand_values\n",
    "    series = series.astype(int)\n",
    "    return series\n",
    "\n",
    "\n",
    "def draw_hist(titanic):\n",
    "    _ = plt.hist(titanic.Age, \n",
    "        bins = 10, # 指定直方图的的区间, 划分为10组\n",
    "        color = 'steelblue', # 指定填充色\n",
    "        edgecolor = 'k', # 指定直方图的边界色\n",
    "        label = 'age ',# 指定标签\n",
    "        alpha = 0.7 )# 指定透明度\n",
    "\n",
    "    _ = plt.legend()\n",
    "    \n",
    "    plt.show()\n",
    "\n",
    "\n",
    "def explore():\n",
    "\n",
    "    titanic = pd.read_csv('./train.csv')\n",
    "    titanic_test = pd.read_csv('./test.csv')\n",
    "\n",
    "    titanic.head()\n",
    "\n",
    "\n",
    "    print('-'*20 , 'info', '-'*20)\n",
    "    print(titanic.info())\n",
    "\n",
    "    print('-'*20 , 'describe', '-'*20)\n",
    "    print(titanic.describe())\n",
    "\n",
    "\n",
    "    print('-'*20, 'isnull', '-'*20)\n",
    "    #查看哪些记录包含无效数据(null)\n",
    "    print(titanic.isnull().sum())\n",
    "\n",
    "    # 过滤掉价值不大且不完整的字段, 如 Cabin 座舱号\n",
    "    titanic = titanic.drop(['Cabin'], axis=1)\n",
    "\n",
    "    # 为 null 值设置随机值\n",
    "    print(\"age is null\", titanic[\"Age\"].isnull().sum())\n",
    "    titanic['Age'] = assign_random_values(titanic['Age'])\n",
    "    print(\"age is null\", titanic[\"Age\"].isnull().sum())\n",
    "\n",
    "    # 过滤掉没有年龄的记录\n",
    "    #titanic.dropna(subset=['Age'], inplace=True)\n",
    "    \n",
    "    \n",
    "    titanic.groupby('Embarked').count()\n",
    "    \n",
    "    return titanic\n",
    "\n",
    "    \n",
    "if __name__ == \"__main__\":\n",
    "    titanic = explore()\n",
    "    draw_hist(titanic)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
